import {createI18n} from "vue-i18n";
import cache from "@/utils/cache";

import en from "element-plus/es/locale/lang/en";
import zhCn from "element-plus/es/locale/lang/zh-cn";
import zhTw from "element-plus/es/locale/lang/zh-tw";

// 定义多语言文本
export const messages: {[k: string]: any} = {
  'en': {...en},
  'zh-cn': {...zhCn},
  'zh-tw': {...zhTw},
}

// 导入./default目录语言文件
const defaults = import.meta.glob('./default/*.ts', {import: 'default', eager: true})
// 导入./modules目录语言文本
const forms = import.meta.glob('./form/*.ts', {import: 'default', eager: true})
// 载入
const languages:{[k: string]: any} = {...defaults, ...forms}
Object.keys(languages).forEach((key: string) => {
    const obj: {[k: string]:any}= languages[key] // 好像已经含有el
    const locale = key.substring(key.lastIndexOf('/') + 1, key.lastIndexOf('.ts'))
    messages[locale] = {...messages[locale], ...obj};
})

// 创建 i18n
// https://kazupon.github.io/vue-i18n/zh/api/#%E6%89%A9%E5%B1%95-vue
export const i18n = createI18n({
    missing: ()=>{}, // 翻译文件中不存在该键时的警告处理
    legacy: false,
    locale: cache.getConfiguration().language,
    fallbackLocale: "zh-cn",
    globalInjection: true,
    messages,
});

export default i18n;
